IPython/Notebook essentials

A notebook is a document formed by cells; each cell can contain Python code to execute (cell type Code) or documentation (cell type Markdown, also including Heading markdown). There are also other, more exotic, cell types.

A cell is executed by pressing Ctrl-RETURN on it (or Shift-RETURN to execute and advance to the next cell). The outcome of the computation (typically the result of the last statement) is output to the notebook (unless the statement is ended by a semicolon). Explicit output is also possible, using print statements.

The top menu in a notebook has a Help entry with generic information on the notebook interface, as well as links to the documentation for IPython and for relevant Python libraries. First-time users should start by clicking on the User Interface Tour in that Help menu.

"Code" notebook cells execute IPython code. IPython is standard Python, with facilities added for interactive execution. It also provides online help at any moment. To launch it, use the question mark. Try it now by executing the following cell:


In [1]:
?

It is also possible to obtain help for specific Python methods, classes or functions. For instance, execute the following cell:


In [ ]:
?str.split

There is also contextual help (tooltips) and autocompletion:

  • <shift-TAB> brings in Python documentation for the object just before the cursor, by opening a help window (pressing <shift-TAB> again will enlarge that window)
  • TAB performs automatic completion (or shows a list for selection) for methods, variables and arguments

In [ ]:
str.split  # <--- try putting the edit cursor at the end and pressing <shift-TAB> or <TAB>

For people new to notebooks, please use now the Help/User Interface Tour menu entry to get a feeling on the capabilities of the user interface.

Magics

IPython also provides magics, which are non-Python commands that start with a % (line magic) or a %% (cell magic) and perform some special processing, like configuring some notebook feature, or running code in other languages

The %lsmagic command provides a list of all defined magics. Execute the following cell:


In [ ]:
%lsmagic

Extensions

A notebook deployment may contain extensions, which add additional functionality to the standard base software.

This deployment comes with two such extensions preinstalled:

  • a ToC (Table of Contents) extension, which can display all the headings in a notebook as a floating menu. This comes especially handy to navigate long notebooks. The extension adds a small ToC icon to the right of the toolbar. Click on it to activate/deactivate the ToC for the current notebook.
  • A "Toggle headers" button added to the right side of the menubar. This switches on/off at the same time both the heading and the toolbar, maximizing space for the notebook.

These extensions have also keyboard shortcuts.

The deployment also contains a Search bar extension (to be able to search strings within all cells in a notebook, and optionally also replace them). But it is deactivated by default, since Jupyter 4.1 already comes with a find/replace menu option (in the Edit menu).

Memory management

Important: Since every notebook started launches a kernel with Spark support, it consumes a sizable amount of memory. Therefore there is a not-too-high limit on the number of notebooks that can be running at the same time. If that limit is exceeded, an error will be generated. There are two solutions:

  • Close some of the open notebooks (shutdown their kernels). This can be done in the notebook browser.
  • Increase the RAM assigned to the virtual machine running the notebooks. This can be adjusted in the Vagrantfile and is 2 GB by default